﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace Office365.SharePointOnline.Cmdlets
{
    public class AuthenticationHelper
    {
        public string AuthenticationMethod { get; set; }
        public string User { get; set; }
        public string Password { get; set; }
        public string Domain { get; set; }

        // see: http://stefanoricciardi.com/2009/09/25/service-locator-pattern-in-csharpa-simple-example/
        public ICredentials GetCredentials()
        {
            ICredentials credentials;
            IServiceLocator locator = new AuthenticationServiceLocator();
            switch (AuthenticationMethod)
            {
                case "SharePointOnline":
                    var credentialServiceSharePointOnline = locator.GetService<SharePointOnlineService>();
                    credentialServiceSharePointOnline.Username = User;
                    credentialServiceSharePointOnline.Password = Password;
                    credentials = credentialServiceSharePointOnline.Authenticate();
                    break;
                case "OnPremise":
                    locator = new AuthenticationServiceLocator();
                    var credentialServiceOnPremise = locator.GetService<OnPremiseService>();
                    credentialServiceOnPremise.Username = User;
                    credentialServiceOnPremise.Password = Password;
                    credentialServiceOnPremise.Domain = Domain;
                    credentials = credentialServiceOnPremise.Authenticate();
                    break;
                default:
                    throw new ArgumentException(string.Format("Unknown authentication method argument: {0}. Valid arguments are SharePointOnline and OnPremise.", AuthenticationMethod));
            }
            return credentials;
        }
    }
}
